<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	<title>新增出租 - ${siteName}</title>
	<meta name="description" content="">
	<meta name="author" content="xuzewei">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
	<link rel="stylesheet" type="text/css" href="css/cloud-admin.css" >
	<link rel="stylesheet" type="text/css" href="css/themes/default.css" id="skin-switcher" >
	<link rel="stylesheet" type="text/css" href="css/responsive.css" >
	<link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css">
	<!-- JQUERY UI-->
	<link rel="stylesheet" type="text/css" href="js/jquery-ui-1.10.3.custom/css/custom-theme/jquery-ui-1.10.3.custom.min.css" />
	<!-- DATE RANGE PICKER -->
	<link rel="stylesheet" type="text/css" href="js/bootstrap-daterangepicker/daterangepicker-bs3.css" />
	<!-- JQGRID -->
	<link rel="stylesheet" type="text/css" href="js/jqgrid/css/ui.jqgrid.min.css" />
	<!-- HUBSPOT MESSENGER -->
	<link rel="stylesheet" type="text/css" href="js/hubspot-messenger/css/messenger.min.css" />
	<link rel="stylesheet" type="text/css" href="js/hubspot-messenger/css/messenger-theme-flat.min.css" />
	<link rel="stylesheet" type="text/css" href="css/base.css" />
	<link rel="stylesheet" type="text/css" href="css/main.css" />
	<style type="text/css">
		.leaseimg1{
			background-color: #ffffff;
			}
		.leaseimg2{
			background-color:#708090;
		}
		.leaseimg3{
			background-color:#00FFFF;
		}
	</style>
</head>
<body id="app" ng-app="app" ng-controller="ctrl">
	<!-- HEADER -->
	<jsp:include page="include/header.jsp" />
	<!--/HEADER -->
	<!-- PAGE -->
	<section id="page">
		<!-- SIDEBAR -->
		<jsp:include page="include/sidebar.jsp" />
		<!-- /SIDEBAR -->
		<div id="main-content">
			<div class="container">
				<div class="row">
					<div id="content" class="col-lg-12">
						<!-- PAGE HEADER-->
						<div class="row">
							<div class="col-sm-12">
								<div class="page-header">
									<!-- BREADCRUMBS -->
									<ul class="breadcrumb">
										<li>
											<i class="fa fa-home"></i>
											<a href="welcome.jsp">Home</a>
										</li>
									</ul>
									<!-- /BREADCRUMBS -->
									<div class="clearfix">
										<h3 class="content-title pull-left">新增出租</h3>
									</div>
									<div class="description"></div>
								</div>
							</div>
						</div>
						<!-- /PAGE HEADER -->
						<div class="box border orange">
							<div class="box-title">
								<h4><i class="fa fa-bars"></i>新增出租</h4>
							</div>
							<div class="box-body big">
								<form role="form">
									<div class="form-group">
									  <label>公司名称</label>
									  <select ng-model="entrepreneurId"  class="form-control" ng-options="o.entrepreneurId as o.name for o in allEntrepreneur" ng-change="queryByNameEntrepreneur()">
								    	<option value="">-- 请选择公司 --</option>
							    	</select>
									</div>
									<div class="form-group">
									  <label>联系人</label>
									  <input type="text" class="form-control" ng-model="Entrepreneur.linkman" readonly="ture">
									  <input ng-model="Entrepreneur.entrepreneurId" style="display:none;">
									</div>
									<div class="form-group">
									  <label>联系方式</label>
									  <input type="text" class="form-control" ng-model="Entrepreneur.phone" readonly="ture">
									</div>
									<div class="form-group" >
										<table id="leasetab" class="addr_table" cellpadding="0" cellspacing="0">
										</table>
									</div>
									<div class="form-group" >
									<label style="width: 60px;height: 20px; background-color:#00CED1;"></label>您已选择的工位
									<label style="width: 60px;height: 20px; background-color:#708090;"></label>已被占用
									</div>
									<div class="form-group">
									  <label>租金标准(元/月)</label>
									  <input id="sumRental1" type="text" class="form-control"  placeholder="租金标准" readonly='true'>
									</div>
									<div class="form-group">
									  <label>租用开始时间</label>
									  <input id="startTime" type="text" class="form-control" ng-model="startTime"  readonly="readonly" placeholder="时间格式20160606">
									</div>
									<div id="monthNumbers" class="form-group">
									  <label>租用时长</label>
									  <select id="monthNumber"   class="form-control" ng-model="monthNumber" onchange="addmulMonth()" >
							    		<option value="0">-- 请选择月数 --</option>
							    		<option value="1">1个月</option>
							    		<option value="2">2个月</option>
							    		<option value="3">3个月</option>
							    		<option value="4">4个月</option>
							    		<option value="5">5个月</option>
							    		<option value="6">6个月</option>
							    		<option value="7">7个月</option>
							    		<option value="8">8个月</option>
							    		<option value="9">9个月</option>
							    		<option value="10">10个月</option>
							    		<option value="11">11个月</option>
							    		<option value="12">12个月</option>
							    	</select>
									</div>
									<div class="form-group">
									  <label>租用结束时间</label>
									  <input id="endTime" type="text" class="form-control" ng-model="endTime" placeholder="终止日期" readonly='true'>
									</div>
									<div id="totalPrices" class="form-group">
									  <label>总价(元)</label>
									  <input id="totalPrice" type="text" class="form-control" ng-model="totalPrice" placeholder="租金标准" readonly='true'>
									</div>
									<button type="submit" class="btn btn-danger" ng-click="submit()">提交</button>
								</form>
							</div>
						</div>
						<div class="footer-tools">
							<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
						</div>
					</div><!-- /CONTENT-->
				</div>
			</div>
		</div>
	</section>
	<!--/PAGE -->
	
	<!-- JAVASCRIPTS -->
	<!-- Placed at the end of the document so the pages load faster -->
	<!-- JQUERY -->
	<script src="js/jquery/jquery-2.0.3.min.js"></script>
	<!-- JQUERY UI-->
	<script src="js/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script>
	<!-- BOOTSTRAP -->
	<script src="bootstrap-dist/js/bootstrap.min.js"></script>
	<!-- DATE RANGE PICKER -->
	<script src="js/bootstrap-daterangepicker/moment.min.js"></script>
	<script src="js/bootstrap-daterangepicker/daterangepicker.min.js"></script>
	<!-- SLIMSCROLL -->
	<script type="text/javascript" src="js/jQuery-slimScroll-1.3.0/jquery.slimscroll.min.js"></script>
	<script type="text/javascript" src="js/jQuery-slimScroll-1.3.0/slimScrollHorizontal.min.js"></script>
	<!-- BLOCK UI -->
	<script type="text/javascript" src="js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
	<!-- JQGRID -->
	<script type="text/javascript" src="js/jqgrid/js/grid.locale-en.min.js"></script>
	<script type="text/javascript" src="js/jqgrid/js/jquery.jqGrid.min.js"></script>
	<!-- COOKIE -->
	<script type="text/javascript" src="js/jQuery-Cookie/jquery.cookie.min.js"></script>
	<!-- HUBSPOT MESSENGER -->
	<script type="text/javascript" src="js/hubspot-messenger/js/messenger.min.js"></script>
	<script type="text/javascript" src="js/hubspot-messenger/js/messenger-theme-flat.js"></script>
	<script type="text/javascript" src="js/hubspot-messenger/js/messenger-theme-future.js"></script>
	<script src="js/script.js"></script>
	<!-- ANGULAR-LIBRARY -->
	<script src="angular-1.5.0-rc.2/angular.js"></script>
	<!-- CUSTOM SCRIPT -->
	<script src="include/common.js"></script>
	
	<script src="laydate/laydate.js"></script>
	<script>
	var sumRental=0;
	var app = angular.module("app", []);
	app.controller('ctrl', function($scope, $http, $httpParamSerializer, $injector, $window) {
		//提交
		$scope.submit = function() {
		 	var items=document.getElementsByName("stationIds");
			var arr=new Array();
			var i=0;
				for(var x=0;x<items.length;x++){
					if(items[x].checked){
						 arr[i] = items[x].value;
						 i++;
						}
					}
			if(arr.length==0||arr.length<0){
				showMsg("至少选一个");
			}else{ 
				var reqDate;
				if(fagle){
					//已有租出表
					reqDate = jQuery.param({
						"stationIds":arr,
						"totalPrice":0,
						"endTime":document.getElementById("endTime").value,	
						"startTime":jQuery("#startTime").val(),	
						"entrepreneurId":$scope.Entrepreneur.entrepreneurId,
						"monthNumber":0,
						"sumRental":document.getElementById("sumRental1").value
					});
				}else{
					//无出租表第一次租工位
					reqDate = jQuery.param({
						"stationIds":arr,
						"totalPrice":document.getElementById("totalPrice").value,
						"endTime":document.getElementById("endTime").value,	
						"startTime":jQuery("#startTime").val(),	
						"entrepreneurId":$scope.Entrepreneur.entrepreneurId,
						"monthNumber":$scope.monthNumber,
						"sumRental":document.getElementById("sumRental1").value
					});
				}
				var reqConfig = { headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} };
				$http.post("api/resourecLease/addLease", reqDate,reqConfig
					).success(function (data) {
				  isErr(data) ? showErrMsg(data) : showMsg('申请工位成功！');
				  $window.location.href = "lease_list.jsp?";
			  });
			}; 
		}
		//获取全部Entrepreneur审核通过的公司
		$scope.queryAllEntrepreneur = function() {
			var reqDate = jQuery.param({});
			var reqConfig = { headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} };
			$http.post(
				"api/allianceEntrepreneur/allEntrepreneurStatus1", reqDate, reqConfig
			).success(function(data, status) {
				if (isErr(data)) {
					showErrMsg(data);
					return;
				}
				$scope.allEntrepreneur = data.response;
			}).error(function(data, status) {
				showNetworkErrMsg(data, status);
			});
		};
		
		//通过公司id查询Entrepreneur
		$scope.queryByNameEntrepreneur = function() {
			if($scope.entrepreneurId==null){
				return;
			}
			var reqDate = jQuery.param({
				"entrepreneurId":$scope.entrepreneurId
			});
			var reqConfig = { headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} };
			$http.post(
				"api/resourecLease/queryByIdEntrepreneur", reqDate, reqConfig
			).success(function(data, status) {
				if (isErr(data)) {
					showErrMsg(data);
					return;
				}
				$scope.Entrepreneur = data.response.entrepreneur;
				var lease=data.response;
				if(lease.lesae!=null&&lease.lesae!="undefined"){
					var time=lease.lesae.endTime;
					jQuery("#monthNumbers").hide();
					jQuery("#totalPrices").hide();
					var time1 = new Date();
					time1.setTime(time);
					var year = time1.getFullYear();
					var month = (time1.getMonth()+1) + "";
					var day = time1.getDate() + "";
					if(month.length==1){
						month="0"+month;
					}
					if(day.length==1){
						day="0"+day;
					}
					if(month=="00"){
						year=parseInt(year)-1;
						month=12;
					}
					var myDate=year+"-"+month+"-"+day;
					$scope.endTime=myDate;
					fagle=true;
				}
			}).error(function(data, status) {
				showNetworkErrMsg(data, status);
			});
		};
		
		//生成工位图
		$scope.stationImg=function(){
			var reqConfig = { headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} };
			$http.post(
				"api/resourecLease/stationImg", reqConfig
			).success(function(data, status) {
				if (isErr(data)) {
					showErrMsg(data);
					return;
				}
				var req=data.response;
				if(req.length>0){
					var b=parseInt(req[0].totalLines);
					var a=parseInt(req[0].totalRows);
				$scope.reqs=data.response; 
				jQuery('#leasetab').empty();
				for(i=0;i<b;i++){
					jQuery('#leasetab').append("<tr id='tr"+i+"'></tr>");
				for(j=0;j<a;j++){
					 if(data.response[(i*a)+j]!=null){
						if(data.response[(i*a)+j].state!=0){
							jQuery("#tr"+i).append("<td id= td"+data.response[(i*a)+j].name+" class='leaseimg2' >"+data.response[(i*a)+j].name+"</td>");
								}else{
										var f="s"+i+j;
										var val=name+i+j+","+i+","+j;
										jQuery("#tr"+i).append("<td id='td"+f+"'>"+data.response[(i*a)+j].name+"</td>");
										jQuery("#td"+f).attr('onclick','sumMoney("'+f+'");');
										jQuery("#td"+f).append("<input type='checkbox' id=stationId"+f+" name='stationIds' value="+data.response[(i*a)+j].stationId+" style='display:none;'>" );
										jQuery("#td"+f).append("<input id='inp"+f+"' value="+data.response[(i*a)+j].rental+" style='display:none;'>");
										jQuery("#td"+f).append("<input id='status"+f+"' value='0' style='display:none;'>");
										jQuery("#checkbox"+f).attr("value",val);
								}
							}else{
								jQuery("#tr"+i).append("<td >该工位已被删除</td>");
							}
						}
					}
				}
			}).error(function(data, status) {
				showNetworkErrMsg(data, status);
			});
		}
		
		$scope.logout = function() {
			logout($window.zone);
		};
	});
	//计算价格和
	function sumMoney(value){
		var stat=jQuery("#status"+value).val();
		if(stat==0){
			document.getElementById("td"+value).style.background= "#00CED1";
			var rental=document.getElementById("inp"+value).value;
			var b=parseFloat(rental);
			sumRental=b+sumRental;
			document.getElementById("sumRental1").value=sumRental;
			//封装lease数据
			var stationStatus=document.getElementById("stationId"+value);
			stationStatus.checked=true;
			document.getElementById("status"+value).value=1;
			addmulMonth();
		}else{
			document.getElementById("td"+value).style.background= "#ffffff";
			var rental=document.getElementById("inp"+value).value;
			var b=parseFloat(rental);
			sumRental=sumRental-b;
			document.getElementById("sumRental1").value=sumRental;
			//删除lease数据
			var stationStatus=document.getElementById("stationId"+value);
			stationStatus.checked=false;
			document.getElementById("status"+value).value=0;
			addmulMonth();
		}
	};
	var fagle=false;
	//年月日相加 和需要的总价格
	function  addmulMonth(){
		if(fagle){
			return;
		}
		// n个月后
		var str=$("#startTime").val();
		var monthNumber=parseInt($("#monthNumber").val());
		var time = new Date(Date.parse(str.replace(/-0/g, "-")));
		time.setMonth(time.getMonth() + (monthNumber + 1))
		var time1 = new Date();
		time1.setTime(time);
		var year = time1.getFullYear();
		var month = time1.getMonth() + "";
		var day = time1.getDate() + "";
		if(month.length==1){
			month="0"+month;
		}
		if(day.length==1){
			day="0"+day;
		}
		if(month=="00"){
			year=parseInt(year)-1;
			month=12;
		}
		var myDate=year+"-"+month+"-"+day;
		$("#endTime").val(myDate);
		//计算总花费
		var sumRental=document.getElementById("sumRental1").value;
		var sumRental1=parseInt(sumRental);
		var totalPrice=sumRental1*monthNumber;
		document.getElementById("totalPrice").value=totalPrice;
	} 
	</script>
	<script>
	jQuery(document).ready(function() {
		App.init();
		jQuery('#nav_resource-manage').click();
		setTimeout(function () { 
			var scope = angular.element('#app').scope();
			scope.queryAllEntrepreneur();
			scope.stationImg();
		}, 500);
	});
	
	;!function(){

		//laydate.skin('molv');

		laydate({
		   elem: '#startTime'
		})

		}();
	</script>
	<!-- /JAVASCRIPTS -->
</body>
</html>